package com.smugglersend.app;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

import static com.smugglersend.app.R.layout.activity_main;

public class MainActivity extends FragmentActivity {

    private boolean mIsBound = false;
    private MusicService mServ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
      //  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        setContentView(R.layout.activity_main);
        MainScreenFontMethod();

        //Drugs Page
        TextView gameOpt = (TextView) findViewById(R.id.new_game);
        gameOpt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent GameOptions = new Intent(MainActivity.this, Drugs.class);
                startActivity(GameOptions);

            }
        });

    // call settings page
    ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton2);
    imageButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)  {
            // Call Android settings
            Intent SettingsActivity = new Intent (MainActivity.this, SettingsActivity.class);
            startActivity(SettingsActivity);
            }
    });
    // Manage music playback
      SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
      boolean music = sharedPref.getBoolean("pref_music", true);
       if(music==true){
       //    MusicService mServ = new MusicService();
           Intent Mstart = new Intent();
           doBindService();
           Mstart.setClass(this, MusicService.class);
           startService(Mstart);
       } else {
           doUnbindService();
       }
           // mp = MediaPlayer.create(MainActivity.this, R.raw.fifty);
       //     mp.setLooping(true);
       //     mp.setVolume(100, 100);
       //     mp.start();
       // } else {
       //     mp.pause();
       // }

    }


   //binding Activity to the service.
        private ServiceConnection Scon = new ServiceConnection(){
        public void onServiceConnected(ComponentName name, IBinder
                binder) {
            mServ = ((MusicService.ServiceBinder)binder).getService();
        }

        public void onServiceDisconnected(ComponentName name) {
            mServ = null;
        }
       };

       void doBindService(){
        bindService(new Intent(this,MusicService.class),Scon,Context.BIND_AUTO_CREATE);
        mIsBound = true;
       }

        void doUnbindService(){
            if(mIsBound)
             {
                unbindService(Scon);
                 mIsBound = false;
              }
     }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }

    private void MainScreenFontMethod() {
        // Font in main
        CustomFontsLoader application = (CustomFontsLoader) getApplication();
        TextView myTextView = (TextView) findViewById(R.id.custom_font);
        application.setTypeface(myTextView);
        TextView myTextView1 = (TextView) findViewById(R.id.custom_font1);
        application.setTypeface(myTextView1);

    }


  //  @Override
 //   public void onPause(){
//        mp.pause();
//      if(this.isFinishing()){

  //    }
 //       Context context = getApplicationContext();
 //       ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
 //     List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
 //       if(!taskInfo.isEmpty()){
 //           ComponentName topActivity = taskInfo.get(0).topActivity;
 //           if(!topActivity.getPackageName().equals(context.getPackageName())) {
 //               mp.release();
 //           }
 //       }
 //       super.onPause();
 //   }


 //   @Override
 //   protected void onDestroy() {
 //       super.onDestroy();

   // }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return super.onOptionsItemSelected(item);
    }

}
